package com.company.oa.announcement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.announcement.dto.AnnouncementDTO;
import com.company.oa.announcement.entity.Announcement;
import com.company.oa.announcement.service.AnnouncementService;
import com.company.oa.common.api.Result;
import com.company.oa.common.utils.SecurityUtils;
import com.company.oa.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/announcements")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    /**
     * 分页查询公告列表
     */
    @GetMapping("/page")
    @RequiresPermissions("announcement:list")
    public Result<IPage<Announcement>> getAnnouncementPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            Announcement announcement) {
        IPage<Announcement> pageResult = announcementService.getAnnouncementPage(page, size, announcement);
        return Result.success(pageResult);
    }

    /**
     * 获取公告详情
     */
    @GetMapping("/{id}")
    public Result<Announcement> getAnnouncementDetail(@PathVariable Long id) {
        Announcement announcement = announcementService.getAnnouncementDetail(id);
        return Result.success(announcement);
    }

    /**
     * 创建公告
     */
    @PostMapping
    @RequiresPermissions("announcement:create")
    public Result<Boolean> createAnnouncement(@RequestBody @Valid AnnouncementDTO announcementDTO) {
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();
        Long deptId = SecurityUtils.getDeptId();
        String deptName = SecurityUtils.getDeptName();

        boolean result = announcementService.createAnnouncement(announcementDTO, userId, userName, deptId, deptName);
        return Result.success(result);
    }

    /**
     * 更新公告
     */
    @PutMapping
    @RequiresPermissions("announcement:update")
    public Result<Boolean> updateAnnouncement(@RequestBody @Valid AnnouncementDTO announcementDTO) {
        boolean result = announcementService.updateAnnouncement(announcementDTO);
        return Result.success(result);
    }

    /**
     * 删除公告
     */
    @DeleteMapping("/{id}")
    @RequiresPermissions("announcement:delete")
    public Result<Boolean> deleteAnnouncement(@PathVariable Long id) {
        boolean result = announcementService.deleteAnnouncement(id);
        return Result.success(result);
    }

    /**
     * 发布公告
     */
    @PostMapping("/{id}/publish")
    @RequiresPermissions("announcement:publish")
    public Result<Boolean> publishAnnouncement(@PathVariable Long id) {
        boolean result = announcementService.publishAnnouncement(id);
        return Result.success(result);
    }

    /**
     * 撤销公告
     */
    @PostMapping("/{id}/cancel")
    @RequiresPermissions("announcement:cancel")
    public Result<Boolean> cancelAnnouncement(@PathVariable Long id) {
        boolean result = announcementService.cancelAnnouncement(id);
        return Result.success(result);
    }

    /**
     * 置顶/取消置顶公告
     */
    @PostMapping("/{id}/top")
    @RequiresPermissions("announcement:top")
    public Result<Boolean> topAnnouncement(@PathVariable Long id, @RequestParam boolean isTop) {
        boolean result = announcementService.topAnnouncement(id, isTop);
        return Result.success(result);
    }

    /**
     * 获取用户的公告列表
     */
    @GetMapping("/user")
    public Result<List<Announcement>> getUserAnnouncements() {
        Long userId = SecurityUtils.getUserId();
        Long deptId = SecurityUtils.getDeptId();
        List<Long> roleIds = SecurityUtils.getRoleIds();

        List<Announcement> announcements = announcementService.getUserAnnouncements(userId, deptId, roleIds);
        return Result.success(announcements);
    }

    /**
     * 阅读公告
     */
    @PostMapping("/{id}/read")
    public Result<Boolean> readAnnouncement(@PathVariable Long id, HttpServletRequest request) {
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();
        String ip = request.getRemoteAddr();

        boolean result = announcementService.readAnnouncement(id, userId, userName, ip);
        return Result.success(result);
    }

    /**
     * 获取公告统计数据
     */
    @GetMapping("/stats")
    @RequiresPermissions("announcement:stats")
    public Result<Map<String, Object>> getAnnouncementStats() {
        Map<String, Object> stats = announcementService.getAnnouncementStats();
        return Result.success(stats);
    }
}